package controleur;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Database;
import modele.DatabaseManager;

/**
 * Controleur de l'application
 */
public class Controleur extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("url", null);
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        /*
        try {
        String forward = null;
        String idaction = request.getParameter("action");
        System.out.println("idaction = " + idaction);

        if (request.getParameter("action").equals("authentification")) {
        System.out.println("Login/mdp : ");
        System.out.println(request.getParameter("login"));
        System.out.println(request.getParameter("mdp"));

        System.out.println("auth");
        forward = authentifier(request);
        } else if ("accueil".equals(idaction)) {
        System.out.println("accueil");
        forward = accueil(request);
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher(forward);
        System.out.println("forward vers " + forward);
        dispatcher.forward(request, response);
        } finally {
        out.close();
        }
         */
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Requête GET");
        Database.setConnectionData("boutique", "root", "");

        List<String> lstDatabases = DatabaseManager.getDatabases();


        for (int i = 0; i < lstDatabases.size(); ++i) {
            System.out.println(i + " DATABASE " + lstDatabases.get(i));
        }

        List<String> tables = DatabaseManager.getLstTables();

        System.out.println("boutique");
        System.out.println("  " + tables.size() + " table(s)");
        for (int i = 0; i < tables.size(); ++i) {
            System.out.println("  " + i + " TABLE " + tables.get(i));
            List<String> columns = DatabaseManager.getLstColumns(tables.get(i));
            System.out.println("  " + columns.size() + " colonne(s)");
            for (int j = 0; j < columns.size(); ++j) {
                System.out.println("    " + j + " COLUMN " + columns.get(j));
                List<String> lines = DatabaseManager.getTableByField(tables.get(i), columns.get(j));
                System.out.println("    " + lines.size() + " ligne(s)");
                for (int k = 0; k < lines.size(); ++k) {
                    System.out.println("      " + k + " VALUE " + lines.get(k));
                }
            }
        }

        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Requête POST");
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>PUT</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Requête PUT");
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>DELETE</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Requête DELETE");
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /**
     * Méthode renvoyant l'utilisateur à l'accueil
     * @param request La requête
     * @return Le forward
     */
    private String accueil(HttpServletRequest request) {
        String forward;
        forward = "VueMenu.jsp";

        return forward;
    }
}
